package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


    @Resource
    private WmChannelMapper wmChannelMapper;


    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult<List<WmChannel>> findAll() {
        return ResponseResult.okResult(list());
    }

    @Override
    public ResponseResult<List<WmChannel>> myList(ChannelDto channelDto) {
        String name = channelDto.getName();
        Integer page = channelDto.getPage();
        Integer size = channelDto.getSize();

        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(WmChannel::getName,name).orderByDesc(WmChannel::getCreatedTime);

        IPage<WmChannel> iPage = new Page<>();

        iPage.setSize(size);
        iPage.setCurrent(page);

        IPage<WmChannel> wmChannelIPage = wmChannelMapper.selectPage(iPage, queryWrapper);

        List<WmChannel> records = wmChannelIPage.getRecords();

        return ResponseResult.okResult(records);
    }

    @Override
    public ResponseResult<String> del(Integer id) {
        WmChannel wmChannel = wmChannelMapper.selectById(id);
        Boolean status = wmChannel.getStatus();
        if (!status) {
            wmChannelMapper.deleteById(id);
            return ResponseResult.okResult("删除成功！");
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.OPERATE_ERROR,"只有被禁用的频道才能删除！");
        }
    }

    @Override
    public ResponseResult<String> mySave(WmChannel channel) {
        String name = channel.getName();
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmChannel::getName,name);
        List<WmChannel> wmChannels = wmChannelMapper.selectList(queryWrapper);
        if (wmChannels.size()>1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ERROR_SQL);
        } else if (wmChannels.size() == 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名称不能重复！");
        } else {
            wmChannelMapper.insert(channel);
            return ResponseResult.okResult("添加成功");
        }
    }

    @Override
    public ResponseResult<String> myUpdate(WmChannel channel) {
        //todo 这里如果频道被引用则不能禁用
        wmChannelMapper.updateById(channel);
        return ResponseResult.okResult("修改成功！");
    }
}
